﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.Common;
using MarketOnlineDTO;
using MySql.Data.MySqlClient;

namespace MarketOnlineDAO
{
    public class LoaiSanPhamDAO : BaseDataProvider
    {
        MySqlCommand _cmd;

        public Boolean ThemLoaiSanPham(LoaiSanPhamDTO lsp)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "insert into LoaiSanPham(TenLoaiSP) values(@TenLoaiSP)";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenLoaiSP", lsp.TenLoaiSP));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean XoaLoaiSanPham(LoaiSanPhamDTO lsp)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "delete from LoaiSanPham where MaLoaiSP = @MaLoaiSP";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiSP", lsp.MaLoaiSP));

            return ExecuteNonQuery(_cmd);
        }

        public Boolean CapNhatLoaiSanPham(LoaiSanPhamDTO lsp)
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "update LoaiSanPham set TenLoaiSP = @TenLoaiSP where MaLoaiSP = @MaLoaiSP";
            _cmd.CommandType = CommandType.Text;
            _cmd.Parameters.Add(new MySqlParameter("@TenLoaiSP", lsp.TenLoaiSP));
            _cmd.Parameters.Add(new MySqlParameter("@MaLoaiSP", lsp.MaLoaiSP));

            return ExecuteNonQuery(_cmd);
        }

        public List<LoaiSanPhamDTO> LayDanhSachLoaiSanPham()
        {
            _cmd = CreateMysqlCommand();
            _cmd.CommandText = "select * from LoaiSanPham";
            _cmd.CommandType = CommandType.Text;

            DataTable dt = new DataTable();

            dt = ExecuteSelectCommand(_cmd);

            if (dt.Rows.Count <= 0)
            {
                return null;
            }

            List<LoaiSanPhamDTO> list = new List<LoaiSanPhamDTO>();

            foreach (DataRow row in dt.Rows)
            {
                LoaiSanPhamDTO dto = new LoaiSanPhamDTO();
                dto.MaLoaiSP = int.Parse(row["MaLoaiSP"].ToString());
                dto.TenLoaiSP = row["TenLoaiSP"].ToString();

                list.Add(dto);
            }

            return list;
        }

    }
}
